﻿/*
 * 对象反射类库
 * 完全公开
 * Created By WangZg
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Octopus.Logging.Commons
{
    public sealed class TypeLoader
    {
        private TypeLoader()
        {
        }

        public static string GetFullTypeName(Type type)
        {
            return type.FullName + "," + type.Assembly.FullName;
        }

        public static object CreateInstance(string fullTypeName)
        {
            string[] name = fullTypeName.Split(',');
            if (name == null || name.Length != 2) { throw new LogException(LogException.ConfigAssemblyDeformity,fullTypeName); }
            return CreateInstance(name[1], name[0]);
        }

        public static object CreateInstance(string assembly, string type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(type);
            }

            return Assembly.Load(assembly).CreateInstance(type);
        }

        public static object CreateInstance(string assembly, string type, object[] args)
        {
            return Assembly.Load(assembly).CreateInstance(type, false, BindingFlags.CreateInstance, null, args, null, null);
        }
    }
}
